package com.zhiyou.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zhiyou.dao.RepairDao;
import com.zhiyou.model.Repair;
import com.zhiyou.util.DBUtils;
import com.zhiyou.util.PageBean;

public class RepairDaoImpl implements RepairDao {

	@Override
	public PageBean<Repair> findAll(int pc, int ps) {
		PageBean<Repair> pb = new PageBean<>();
		String sq = "select count(*) from repair "; 
		Object ob[] = {};
		DBUtils db = new DBUtils();
		ResultSet r = db.select(sq, ob);
		pb.setPc(pc);
		pb.setPs(ps);
		int tr = 0;
		if(r==null){
		}else{
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setTr(tr);
		String sql = "select * from repair limit ?,? ";
		Object obj[] = {(pc-1)*ps ,ps};
		ResultSet rs = db.select(sql, obj);
		List<Repair> list = new ArrayList<>();
		if(rs == null ){
		}else{
			try {
				while(rs.next()){
					Repair repair = new Repair(rs.getInt(1), rs.getShort(2), rs.getInt(3), rs.getString(4), rs.getDate(5), rs.getString(6), rs.getString(7));
					list.add(repair);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list);
		db.close();
		return pb;
	}

	@Override
	public Repair findById(int id) {
		String sql = "select * from repair where fr_id = ?";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		ResultSet rs = db.select(sql, obj);
		if(rs == null ){
		}else{
			try {
				while(rs.next()){
					Repair repair = new Repair(rs.getInt(1), rs.getShort(2), rs.getInt(3), rs.getString(4), rs.getDate(5), rs.getString(6), rs.getString(7));
					db.close();
					return repair;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		db.close();
		return null;
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		String sql = "delete from repair where fr_id = ?";
		Object obj[] = {id};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean update(Repair repair) {
		String sql = "update repair  set flat_id = ?,lo_id = ?,fr_explain= ?, fr_time = ?,fr_result = ?,fr_remark = ?  where fr_id = ?";
		Object obj[] = {repair.getFlat_id(),repair.getLo_id(),repair.getFr_explain(),repair.getFr_time(),repair.getFr_result(),repair.getFr_remark(),repair.getFr_id()};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean add(Repair repair) {
		String sql = "insert into repair values(null,?,?,?,?,?,?)";
		Object obj[] = {repair.getFlat_id(),repair.getLo_id(),repair.getFr_explain(),repair.getFr_time(),repair.getFr_result(),repair.getFr_remark()};
		DBUtils db = new DBUtils();
		int num = db.update(sql, obj);
		if(num==1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int[] dels(int[] id) {
		String sql = "delete from repair where fr_id = ?";
		DBUtils db = new DBUtils();
		int nums[] = db.dels(sql, id);
		db.close();
		return nums;
	}

	@Override
	public PageBean<Repair> find(String flat, String lo, int pc, int ps) {
		DBUtils db = new DBUtils();
		PageBean<Repair> pb = new PageBean<>();
		pb.setPc(pc);
		pb.setPs(ps);
		StringBuffer sq = new StringBuffer("select count(*) from repair where 1=1 ");
		List<Object> list = new ArrayList<>();
		if(flat!=null&&!flat.trim().isEmpty()){
			sq.append(" and flat_id like ? ");
			list.add("%"+flat+"%");
		}
		if(lo!=null&&!lo.trim().isEmpty()){
			sq.append(" and lo_id like ? ");
			list.add("%"+lo+"%");
		}
		int tr = 0;
		ResultSet r = db.select(sq.toString(), list.toArray());
		if(r==null){
			pb.setTr(0);
		}else{ 
			try {
				while(r.next()){
					tr = r.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pb.setTr(tr);
		}
		StringBuffer sql = new StringBuffer(sq.toString().replace("count(*)", "*"));
		sql.append(" limit ?,?");
		list.add((pc-1)*ps);
		list.add(ps);
		List<Repair> list1 = new ArrayList<>();
		ResultSet rs = db.select(sql.toString(), list.toArray());
		if (rs==null) {
			
		}else{
			try {
				while(rs.next()){
					Repair repair = new Repair(rs.getInt(1), rs.getShort(2), rs.getInt(3), rs.getString(4), rs.getDate(5), rs.getString(6), rs.getString(7));
					list1.add(repair);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pb.setBeanList(list1);
		db.close();
		return pb;
	}

}
